package bin_search;

public class search {
	public int bin_search (int array[], int n, int x)
	{
		int first = 0;
		int last = n;
		int mid = (last - first) / 2;
		if (last == 0)
		{
			return 0;
		}
		else if (array[0]  > x)
		{
			return 0;
		}
		else if (array[n-1] < x)
		{
			return 0;
		}
		while (first < last)
		{
	        if (x < array[mid])
	        {
	            last = mid;
	        }
	        else if (x == array [mid])
	        {
	        	return mid+1;
	        }
	        else
	        {
	            first = mid + 1;
	        }
	        mid = first + (last - first) / 2;
		}
		return 0;
	}

}
